package aos.ydksxt.service.jcxxcj;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjWbdrFgxx;
import aos.ydksxt.dao.DjWbdrGtxx;
import aos.ydksxt.service.common.CommonService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 国土信息采集导入服务
 * Created by Stiofan on 2017/3/9.
 */
@Service
public class DsfxxdrService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setAttribute("juid", httpModel.getInDto().get("juid"));
        httpModel.setViewPath("ydksxt/jcxxcj/dsfxxdr.jsp");
    }
    @Transactional
    public void gtxxdr(HttpModel httpModel) {
        Dto outDto = Dtos.newOutDto();
        try {
            MultipartFile multipartFile = ((MultipartHttpServletRequest) httpModel.getRequest()).getFile("gtdrwj");
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(multipartFile.getInputStream());
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
            int rows = xssfSheet.getLastRowNum();
            for (int i = 2; i < rows; i++) {
                XSSFRow xssfRow = xssfSheet.getRow(i);
                if (!StringUtils.isEmpty(xssfRow.getCell(1).getStringCellValue())) {
                    Dto params = Dtos.newDto("tdzdh", xssfRow.getCell(1).getStringCellValue());
                    params.put("tdsyqr", xssfRow.getCell(2).getStringCellValue());
                    DjWbdrGtxx oldGtxx = (DjWbdrGtxx) persistenceDAO.findSingleByHql("from DjWbdrGtxx xx where xx.tdzdh=:tdzdh and xx.tdsyqr=:tdsyqr and xx.yxbz='Y'", params);
                    if (oldGtxx != null) {
                        oldGtxx.setXgrDm(getCzryDm(httpModel));
                        oldGtxx.setXgrq(getDateTime());
                        oldGtxx.setYxbz("N");
                        persistenceDAO.saveOrUpdate(oldGtxx);
                    }
                    DjWbdrGtxx newGtxx = new DjWbdrGtxx();
                    newGtxx.setUuid(AOSUtils.getRandomUUID());
                    newGtxx.setLrrDm(getCzryDm(httpModel));
                    newGtxx.setLrrq(getDateTime());
                    newGtxx.setTdzdh(xssfRow.getCell(1).getStringCellValue());
                    newGtxx.setTdsyqr(xssfRow.getCell(2).getStringCellValue());
                    newGtxx.setTdmj(BigDecimal.valueOf(xssfRow.getCell(3).getNumericCellValue()).setScale(2));
                    newGtxx.setTdxz(xssfRow.getCell(4).getStringCellValue());
                    newGtxx.setTdsyqlxmc(xssfRow.getCell(5).getStringCellValue());
                    newGtxx.setTdyt(xssfRow.getCell(6).getStringCellValue());
                    newGtxx.setTdyz(BigDecimal.valueOf(xssfRow.getCell(7).getNumericCellValue()).setScale(2));
                    newGtxx.setTdzldz(xssfRow.getCell(8).getStringCellValue());
                    newGtxx.setTdsyqQsrq(xssfRow.getCell(9).getDateCellValue());
                    newGtxx.setTdsyqZzrq(xssfRow.getCell(10).getDateCellValue());
                    newGtxx.setTdsyqZsbh(xssfRow.getCell(11).getStringCellValue());
                    newGtxx.setSjcjrqQsrq(xssfRow.getCell(12).getDateCellValue());
                    newGtxx.setSjcjrqZzrq(getDateTime());
                    newGtxx.setYxbz("Y");
                    persistenceDAO.saveOrUpdate(newGtxx);
                }
            }
            outDto.setAppCode("suc");
            outDto.setAppMsg("执行导入成功!");
        } catch (IOException e) {
            outDto.setAppCode("err");
            outDto.setAppMsg("执行导入时发生错误：" + e.getMessage());
            logger.error(e);
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void fgxxdr(HttpModel httpModel) {
        Dto outDto = Dtos.newOutDto();
        try {
            MultipartFile multipartFile = ((MultipartHttpServletRequest) httpModel.getRequest()).getFile("fgdrwj");
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(multipartFile.getInputStream());
            XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
            int rows = xssfSheet.getLastRowNum();
            for (int i = 2; i < rows; i++) {
                XSSFRow xssfRow = xssfSheet.getRow(i);
                if (!StringUtils.isEmpty(xssfRow.getCell(9))) {
                    Dto params = Dtos.newDto("fczsbh", xssfRow.getCell(9).getStringCellValue());
                    DjWbdrFgxx oldFgxx = (DjWbdrFgxx) persistenceDAO.findSingleByHql("from DjWbdrFgxx xx where xx.fczsbh=:fczsbh and xx.yxbz='Y'", params);
                    if (oldFgxx != null) {
                        oldFgxx.setXgrDm(getCzryDm(httpModel));
                        oldFgxx.setXgrq(getDateTime());
                        oldFgxx.setYxbz("N");
                        persistenceDAO.saveOrUpdate(oldFgxx);
                    }
                    DjWbdrFgxx newFgxx = new DjWbdrFgxx();
                    newFgxx.setUuid(AOSUtils.getRandomUUID());
                    newFgxx.setLrrDm(getCzryDm(httpModel));
                    newFgxx.setLrrq(getDateTime());
                    newFgxx.setFcmc(xssfRow.getCell(1).getStringCellValue());
                    newFgxx.setFcsyqr(xssfRow.getCell(2).getStringCellValue());
                    newFgxx.setJzmj(BigDecimal.valueOf(xssfRow.getCell(3).getNumericCellValue()).setScale(2));
                    newFgxx.setFcyz(BigDecimal.valueOf(xssfRow.getCell(4).getNumericCellValue()).setScale(2));
                    newFgxx.setFcyt(xssfRow.getCell(5).getStringCellValue());
                    newFgxx.setTdzdh(xssfRow.getCell(6).getStringCellValue());
                    newFgxx.setTdzdh(xssfRow.getCell(7).getStringCellValue());
                    newFgxx.setFcqdrq(xssfRow.getCell(8).getDateCellValue());
                    newFgxx.setFczsbh(xssfRow.getCell(9).getStringCellValue());
                    newFgxx.setYxbz("Y");
                    persistenceDAO.saveOrUpdate(newFgxx);
                }
            }
            outDto.setAppCode("suc");
            outDto.setAppMsg("执行导入成功!");
        } catch (IOException e) {
            outDto.setAppCode("err");
            outDto.setAppMsg("执行导入时发生错误：" + e.getMessage());
            logger.error(e);
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void gtmbxz(HttpModel httpModel) {
        HttpServletResponse httpServletResponse = httpModel.getResponse();
        Dto outDto = Dtos.newOutDto();
        outDto.put("fileName", "国土土地信息采集表模板.xlsx");
        outDto.setAppCode("suc");
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void fgmbxz(HttpModel httpModel) {
        HttpServletResponse httpServletResponse = httpModel.getResponse();
        Dto outDto = Dtos.newOutDto();
        outDto.put("fileName", "房管房产信息采集表模板.xlsx");
        outDto.setAppCode("suc");
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
}
